﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class Module_FormDatVe : PanelControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDDLthang(ddlThangDi);
            bindDDLthang(ddlThangVe);
            bindDDLngay(ddlNgayDI, DateTime.Now.Day, ddlThangDi);
            bindDDLngay(ddlNgayVe, DateTime.Now.Day, ddlThangVe);
            bindDDLLocation(ddlDi);
            bindDDLLocation(ddlDen);
            
        }
        if (rdoMotChieu.Checked)
        {
            ddlNgayVe.Visible = false;
            ddlThangVe.Visible = false;
            
        }
        else if (rdoKhuHoi.Checked)
        {
            ddlNgayVe.Visible = true;
            ddlThangVe.Visible = true;
        }
        if (ddlDen == ddlDi)
            Label1.Text = "Nơi đến và nơi đi không được trùng nhau";
        else
            Label1.Text = "";
    }
    public void bindDDLngay(DropDownList dl,int day,DropDownList thang)
    {
        //string t = thang.SelectedValue.Substring(0, 2);
        //string month="";
        //for(int i=0;i<=t.Length;i++){
        //month+=t[i];
        //}
        for (int i = day; i <= DateTime.DaysInMonth(DateTime.Now.Year, int.Parse(thang.SelectedValue.Substring(0, 2).Replace('/',' '))); i++)
        {
            dl.Items.Add(new ListItem(i.ToString(), i.ToString() ));
        }
    }

    public void bindDDLLocation(DropDownList location)
    {
        
        CityBusiness city = new CityBusiness();
        DataTable dt = city.getAllCity();        
        location.DataSource = dt;
        location.DataTextField = "cityName";
        location.DataValueField = "citySymbol";
        location.DataBind();
        location.Items[0].Selected = true;
    }

    public void bindDDLthang(DropDownList dl)
    {
        int year = DateTime.Now.Year;
        int month = DateTime.Now.Month;
        while ( month <= month+12)
        {
            if (month > 12)
            {
                month = 1;
               year+=1 ;
            }
            
            dl.Items.Add(new ListItem("tháng "+month.ToString()+" năm " + year, month.ToString()+"/"+year));
            month++;
            if (dl.Items.Count == 14)
                break;
        }
        dl.Items[0].Selected=true;
    }
    protected void ddlThangDi_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlNgayDI.Items.Clear();
        this.bindDDLngay(ddlNgayDI, 1, ddlThangDi);
        ddlThangVe.SelectedIndex = ddlThangDi.SelectedIndex;
        ddlNgayVe.Items.Clear();
        this.bindDDLngay(ddlNgayVe, 1, ddlThangVe);
        
    }
    protected void ddlThangVe_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlNgayVe.Items.Clear();
        this.bindDDLngay(ddlNgayVe, 1, ddlThangVe);
    }
    protected void rdoKhuHoi_CheckedChanged(object sender, EventArgs e)
    {

        ddlNgayVe.Visible = true;
        ddlThangVe.Visible = true;
        this.an1.Visible = true;

        DateTime t = Convert.ToDateTime((ddlNgayDI.SelectedValue+ "/"+ ddlThangDi.SelectedValue)).AddDays(5);

        Label1.Text = t.ToShortDateString();
    }
    protected void rdoMotChieu_CheckedChanged(object sender, EventArgs e)
    {
        ddlNgayVe.Visible = false;
        ddlThangVe.Visible = false;
        this.an1.Visible = false;
    }
    protected void ddlNgayDI_SelectedIndexChanged(object sender, EventArgs e)
    {
        int i = int.Parse(ddlNgayDI.SelectedValue);
        ddlNgayVe.SelectedValue = (i + 3).ToString();
    }
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        string ngaydi = ddlNgayDI.SelectedValue+"/" + ddlThangDi.SelectedValue;
        string ngayve = ddlNgayVe.SelectedValue +"/"+ ddlThangVe.SelectedValue;
        string noidi = ddlDi.SelectedValue;
        string noiden = ddlDen.SelectedValue;
        string khuhoi = rdoKhuHoi.Checked.ToString();
        string motchieu = rdoMotChieu.Checked.ToString();
        
        
        
        if (noidi == noiden)
        {           
            Response.Redirect("Default.aspx");            
        }
        else
        {
            Session["datve"] = null;
            BookingEntities be = new BookingEntities();
            be._totalNguoiLon = int.Parse(ddlNguoiLon.SelectedValue.ToString());
            be._totalTreEm = int.Parse(ddlTreEm.SelectedValue.ToString());
            be._khuhoi = rdoKhuHoi.Checked.ToString();
            Session["datve"] = be;
            Response.Redirect("Default.aspx?url=Module/SelectFlight.ascx&ngaydi=" + ngaydi + "&ngayve=" + ngayve +
               "&noidi=" + noidi + "&noiden=" + noiden + "&khuhoi=" + khuhoi + "&motchieu=" + motchieu);
        }
    }
    
}